package org.szy.plugin.dev.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.mybatisflex.core.paginate.Page;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotNull;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.plugin.dev.entity.DevMessage;
import org.szy.plugin.dev.param.DevMessageQueryParam;
import org.szy.plugin.dev.service.DevMessageService;

/**
 * 开发-站内消息控制器
 *
 * @author wangbuhao
 * @since 2023-06-28 18:17:41
 */
@Controller
@Mapping("/dev/message")
public class DevMessageController extends BaseController<DevMessage> {

    private final String PATH_PREFIX = "dev/message/";
    @Inject
    private DevMessageService devMessageService;

    /**
     * 开发-站内消息列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 开发-站内消息编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit(String id) {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        view.put("id", id);
        return view;
    }

    /**
     * 获取开发-站内消息分页数据
     *
     * @author wangbuhao
     * @since 2023-06-28 18:17:41
     */
    @Get
    @Mapping("/page")
    public R<Page<DevMessage>> page(DevMessageQueryParam param) {
        return R.data(devMessageService.page(getPage(), param));
    }

    /**
     * 获取开发-站内消息详情
     *
     * @author wangbuhao
     * @since 2023-06-28 18:17:41
     */
    @Get
    @Mapping("/getInfo")
    public R<DevMessage> getInfo(@NotNull Long id) {
        return R.asNull(devMessageService.getById(id));
    }


    /**
     * 添加开发-站内消息
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    public R<?> add(DevMessage entity) {
        boolean save = devMessageService.save(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑开发-站内消息
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    public R<?> update(DevMessage entity) {
        boolean update = devMessageService.updateById(entity);
        return update ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 删除开发-站内消息
     *
     * @author wangbuhao
     * @since 2023-06-28 18:17:41
     */
    @Delete
    @Mapping("/delete")
    public R<?> delete(@NotNull Long id) {
        boolean delete = devMessageService.delete(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }


    /**
     * 发布
     */
    @Put
    @Mapping("/release")
    @SaCheckPermission(value = "dev_message_release", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> release(@NotNull Long id) {
        boolean modify = devMessageService.release(id);
        return R.bool(modify);
    }

}
